//
//  JKWelcomeViewController.swift
//  JKWeibo
//
//  Created by albert on 16/5/14.
//  Copyright © 2016年 albert. All rights reserved.
//  欢迎界面

import UIKit
import SDWebImage

class JKWelcomeViewController: UIViewController {

    /** 头像 */
    @IBOutlet weak var iconImageView: UIImageView!
    /** 欢迎回来标题label */
    @IBOutlet weak var titleLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.设置头像圆角
        iconImageView.layer.cornerRadius = 45
        //iconImageView.layer.masksToBounds = true
        iconImageView.clipsToBounds = true
        
        // 2.设置头像位置偏移，做弹簧动画
        iconImageView.transform = CGAffineTransformMakeTranslation(0, 100)
        
        // 3.设置头像
        assert(JKUserAccount.loadUserAccount() != nil, "必须授权之后才能显示欢迎界面")
        guard let url = NSURL(string: JKUserAccount.loadUserAccount()!.avatar_large!) else{
            return
        }
        iconImageView.sd_setImageWithURL(url)
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 做动画
        UIView.animateWithDuration(0.75, delay: 0, usingSpringWithDamping: 0.3, initialSpringVelocity: 0, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
            self.iconImageView.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    self.titleLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        // 发出切换到主页的通知
                        NSNotificationCenter.defaultCenter().postNotificationName(JKSwitchRootViewControllerNotification, object: nil, userInfo: [JKSwitchRootViewControllerKey : JKSwitchToMainValue])
                })
        }
    }
}
